/**
 * @project name:crm
 */
package com.yunbo.crm.globalExceptionHandler;

/**
 * @Author : yun&bo
 * @CreateDate : ==>2021/1/20 15:47
 * @ClassName : ==>
 * @Description : ==> 全局异常处理器
 * @Version
 */

import com.yunbo.crm.exceptions.NoLoginException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.servlet.ModelAndView;
import javax.servlet.http.HttpServletRequest;


@ControllerAdvice
public class GlobalExceptionResolver  {
    public static final String DEFAULT_ERROR_VIEW = "error";
    public static final String NO_LOGIN_VIEW = "redirect:/index";

    @ExceptionHandler(NoLoginException.class)
    public ModelAndView noLoginExceptionView(HttpServletRequest req, Exception e) {
        ModelAndView modelAndView = new ModelAndView();
        //将异常信息设置如modelAndView
        modelAndView.setViewName(NO_LOGIN_VIEW);
        //返回ModelAndView
        return modelAndView;
    }


    @ExceptionHandler(Exception.class)
    public ModelAndView viewExceptionHandler(HttpServletRequest req, Exception e) {
        ModelAndView modelAndView = new ModelAndView();
        //将异常信息设置如modelAndView
        modelAndView.addObject("code", 101);
        modelAndView.addObject("msg", e.getMessage());
        modelAndView.setViewName(DEFAULT_ERROR_VIEW);
        //返回ModelAndView
        return modelAndView;
    }


}
